package com.zhuiyun.project.api.teacherReservation.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhuiyun.project.api.course.entity.Course;
import com.zhuiyun.project.api.course.mapper.CourseMapper;
import com.zhuiyun.project.api.course.service.ICourseService;
import com.zhuiyun.project.api.plan.fitnessPlan.model.FitnessPlanModel;
import com.zhuiyun.project.api.plan.fitnessPlanAction.entity.FitnessPlanAction;
import com.zhuiyun.project.api.plan.fitnessPlanAction.model.FitnessPlanActionModel;
import com.zhuiyun.project.api.system.user.entity.SysUser;
import com.zhuiyun.project.api.teacherReservation.mapper.TeacherReservationMapper;
import com.zhuiyun.project.api.teacherReservation.model.*;
import com.zhuiyun.project.api.teacherReservation.service.TeacherReservationService;
import com.zhuiyun.project.common.errorCode.EmErrorCode;
import com.zhuiyun.project.common.response.CommonResult;
import com.zhuiyun.project.security.commonhandler.SecurityLoginUserVerification;
import com.zhuiyun.project.utils.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationServiceException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @ClassName TeacherReservationServiceImpl
 * @Description TODO
 * @Author wuwan
 * @Date 2023/11/14 10:23
 **/
@Service
public class TeacherReservationServiceImpl implements TeacherReservationService {
    @Autowired
    TeacherReservationMapper teacherReservationMapper;
    @Resource
    BCryptPasswordEncoder bCryptPasswordEncoder;

    @Resource
    CourseMapper courseMapper;

    @Resource
    ICourseService iCourseService;
    /*
    * @Author wuwan
    * @Description 查询学员预约信息
    * @Date 2023/11/16 8:42
    * @Return
    */
    @Override
    public CommonResult getReservation(Integer coachId, String reservationStatus, String reservationTime) {
        //设置时间格式
        List<ReservationModel> reservation = teacherReservationMapper.getReservation(coachId, reservationStatus, reservationTime);
        return CommonResult.ok(reservation);
    }
    /*
    * @Author wuwan
    * @Description 开始训练
    * @Date 2023/11/16 8:41
    * @Return 
    */
    @Override
    public CommonResult upReservationStatusOne(UpReservationStatusOneModel upReservationStatusOneModel) {
        Integer integer = teacherReservationMapper.upReservationStatusOne(upReservationStatusOneModel.getId());
        if (integer >= 1) {
            return CommonResult.ok("已开启训练");
        }
        return CommonResult.error("训练开启失败");
    }
    /*
    * @Author wuwan
    * @Description 结束训练方法
    * @Date 2023/11/16 8:41
    * @Return
    */
    @Override
    public CommonResult upReservationStatusTwo(Integer id,Integer tid) {
        Integer integer = teacherReservationMapper.upReservationStatusTwo(id);
        if (integer >= 1) {
            teacherReservationMapper.upTims(tid);
            return CommonResult.ok("已结束训练");
        }
        return CommonResult.error("训练结束失败");
    }
    /*
    * @Author wuwan
    * @Description 修改预约课程信息 如调整预约时间 调整课程安排
    * @Date 2023/11/16 9:35
    * @Return
    */
    @Override
    public CommonResult updateReservation(UpdateReservationModel updateReservationModel) {
        Integer integer = teacherReservationMapper.updateReservation(updateReservationModel);
        if (integer >= 1) {
            return CommonResult.ok("修改成功");
        }
        return CommonResult.error("修改失败");
    }
    /*
    * @Author wuwan
    * @Description 教练端登录方法
    * @Date 2023/11/23 14:01
    * @Return
    */
    @Override
    public CommonResult coachLogin(CoachLoginModel coachLoginModel) {
        // 教练账号
        String coachPhone = coachLoginModel.getCoachPhone();
        // 教练密码
        String coachPassword = coachLoginModel.getCoachPassword();
        //验证账号是否为空
//        SecurityLoginUserVerification.vitParams(coachPhone, EmErrorCode.USERNAME_IS_NULL.getErrMsg());
        if (!ObjectUtils.isNotEmpty(coachPhone)){
            return CommonResult.error(EmErrorCode.USERNAME_IS_NULL.getErrMsg());
        }
        //验证密码是否为空
        if (!ObjectUtils.isNotEmpty(coachPassword)){
            return CommonResult.error(EmErrorCode.PASSWORD_IS_NULL.getErrMsg());
        }
        //获取用户信息
        CoachLoginModel coachName = teacherReservationMapper.getCoachName(coachPhone);
        if (!ObjectUtils.isNotEmpty(coachName)){
            return CommonResult.error(EmErrorCode.USER_NOT_EXIST.getErrMsg());
        }
        // 验证密码
        boolean matches = bCryptPasswordEncoder.matches(coachPassword, coachName.getCoachPassword());
        if(!matches){
            return CommonResult.error(EmErrorCode.USER_PASSWORD_ERROR.getErrMsg());
        }
        return CommonResult.ok(coachName);
    }
    /*
     * @Author wuwan
     * @Description 查询计划绑定的动作
     * @Date 2023/11/21 19:07
     * @Return
     */
    @Override
    public CommonResult selectPlanAction(Integer uid) {
        FitnessPlanModel fitnessPlanModel = teacherReservationMapper.selectUserPlan(uid);
        List<FitnessPlanActionModel> fitnessPlanActionModels = teacherReservationMapper.selectPlanAction(fitnessPlanModel.getId());
        return CommonResult.ok(fitnessPlanActionModels);
    }
    /*
    * @Author wuwan
    * @Description 查询所有动作
    * @Date 2023/11/22 14:45
    * @Return
    */
    @Override
    public CommonResult selectAction() {
        return CommonResult.ok(teacherReservationMapper.selectAction());
    }
    /*
     * @Author wuwan
     * @Description 修改计划中的动作内容（更换动作）
     * @Date 2023/11/23 13:59
     * @Return
     */
    @Override
    public CommonResult updatePlanAction(PlanActionModel planActionModel) {
        Integer integer = teacherReservationMapper.updatePlanAction(planActionModel);
        if (integer == 1){
            return CommonResult.ok("修改成功");
        }
        return CommonResult.error("修改失败");
    }

}
